
//
//  HHFiveViewController.m
//  Flower
//
//  Created by 花花 on 2016/12/6.
//  Copyright © 2016年 花花. All rights reserved.
//

#import "HHFiveViewController.h"
#import "HHScrollPageView.h"
#import "HHTest1ViewController.h"
@interface HHFiveViewController ()<HHScrollPageViewDelegate>

@property(nonatomic, strong) NSArray<NSString *> *titles;
@property(nonatomic, strong) NSArray<UIViewController <HHScrollPageViewChildDelegate>*> *childVc;

@property(nonatomic, weak) HHScrollSegmentView *segmentView;

@property(nonatomic, weak) HHContentView *contentView;

@end

@implementation HHFiveViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    

    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.childVc = [self setupChildVc];
    
    //初始化  注意:顺序不能反
    [self setupSegmentView];
    [self setupContentView];
    

}
-(void)setupSegmentView{
    HHSegmentStyle *style = [[HHSegmentStyle alloc]init];
    //遮盖
    style.showCover = YES;
    // 不要滚动标题, 每个标题将平分宽度
    style.scrollTitle = NO;
    // 渐变
    style.gradualChangeTitleColor = YES;
    // 遮盖背景颜色
    style.coverBackgroundColor =[UIColor whiteColor];
    //标题一般状态颜色 --- 注意一定要使用RGB空间的颜色值
    style.normalTitleColor = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0];
    //标题选中状态颜色 --- 注意一定要使用RGB空间的颜色值
    style.selectedTitleColor = [UIColor colorWithRed:235.0/255.0 green:0.0/255.0 blue:0.0/255.0 alpha:1.0];
    
    self.titles =@[@"国际新闻",@"国内新闻"];
    
    __weak typeof(self)weakSelf = self;
    
    HHScrollSegmentView *segmentView =[[HHScrollSegmentView alloc]initWithFrame:CGRectMake(0, 0, 130, 28) segmentStyle:style delegate:self titles:self.titles titleDidClick:^(HHTitleView *titleView, NSInteger index) {
       
        [weakSelf.contentView setContentOffSet:CGPointMake(weakSelf.contentView.bounds.size.width*index, 0) animated:YES];
        
    }];
    
    segmentView.layer.cornerRadius = 14;
    segmentView.backgroundColor =[UIColor redColor];
    self.segmentView =segmentView;
    
    self. navigationItem.titleView = self.segmentView;
    
}

-(void)setupContentView{

    HHContentView *contentView =[[HHContentView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) segmentView:self.segmentView parentViewController:self delegate:self];
    
    
    self.contentView = contentView;
    [self.view addSubview:self.contentView];

}
-(NSArray *)setupChildVc{

    HHTest1ViewController *test1 =[[HHTest1ViewController alloc]init];
    test1.view.backgroundColor = [UIColor orangeColor];
    
    
    HHTest1ViewController *test2 =[[HHTest1ViewController alloc]init];
    test2.view.backgroundColor = [UIColor yellowColor];

    NSArray *childVc =[NSArray arrayWithObjects:test1,test2, nil];
    return childVc;

    
}
#pragma mark - <HHScrollPageViewChildDelegate>
-(NSInteger)numberOfChildViewControllers{

    return  self.titles.count;
    
    
}

/** 获取到将要显示的页面的控制器
 * -reuseViewController : 这个是返回给你的controller, 你应该首先判断这个是否为nil, 如果为nil 创建对应的控制器并返回, 如果不为nil直接使用并返回
 * -index : 对应的下标
 */
- (UIViewController<HHScrollPageViewChildDelegate> *)childViewController:(UIViewController<HHScrollPageViewChildDelegate> *)reuseViewController forIndex:(NSInteger)index{

    UIViewController<HHScrollPageViewChildDelegate> *childVc = reuseViewController;
    if (!childVc) {
        childVc = self.childVc[index];
    }
    return childVc;
}


-(CGRect)frameOfChildControllerForContainer:(UIView *)containerView {
    return  CGRectInset(containerView.bounds, 20, 20);
}
@end
